/*********************************************************************************
 *      Copyright:  (C) 2022 zengwen<1825984037@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  socket_client_init.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(07/07/22)
 *         Author:  zengwen <1825984037@qq.com>
 *      ChangeLog:  1, Release initial version on "07/07/22 13:43:43"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>	
#include "logger.h"


int socket_client_init(char *ip,int port)
{
	   int                  sockfd=-1;
	   int                  confd=-1;
	   struct sockaddr_in   sockaddr;
	   char                 buf[1024];
	   char                 massage[128];
	   float                temp;
	   char                 pie_ID[128];
	   char                 cur_time[20]={0};
	   int                  rv_read;

	   sockfd=socket(AF_INET,SOCK_STREAM,0);
	   if(sockfd<0)
	   {
		   log_err("Create sockfd %d failure:%s\n",sockfd,strerror(errno));
		   return -2;
	   }
	   log_info("Create sockfd %d succesfully!\n",sockfd);

	   memset(&sockaddr,0,sizeof(sockaddr));
	   sockaddr.sin_family=AF_INET;
	   sockaddr.sin_port=htons(port);
	   inet_aton(ip,(void *)&sockaddr.sin_addr);
	   confd=connect(sockfd,(struct sockaddr *)&sockaddr,sizeof(sockaddr));
	   if(confd<0)
	   {
		   log_err("connect to server [%s:%d] failure:%s\n",ip,port,strerror(errno));
		   return -3;
	   }
	   log_info("connect to server [%s:%d] succesfully!\n",ip,port);
	   return sockfd;
}


